package com.noble.glamour.ui.user.my.auth;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.noble.glamour.R;
import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.bean.config.VideoVerifyBean;
import com.noble.glamour.bean.my.IdCardVerifyStatusInfo;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.base.BaseActivity;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.glamour.module.util.UIShow;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;

/**
 * 真人认证
 */
public class MyAuthenticationAct extends BaseActivity implements View.OnClickListener, RequestComplete, PObserver {
    public static final int AUTHENTICSTION_REQUESTCODE = 103;
    private final int authForVodeo = 104;
    private final int authIDCard = 105;

    private TextView tv_txt_auth_phone, tv_txt_auth_video, tv_txt_auth_id;
    private UserDetail userDetail;
    private VideoVerifyBean videoVerifyBean;
    private IdCardVerifyStatusInfo mIdCardVerifyStatusInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.f1_authentication_act);
        setBackView(getResources().getString(R.string.auth_title));
        initObj();
        initConfig();
        initView();

        //以下必须在 initView() 后面
        phoneShow();
        initVideoAuth();
        initIdCardAuth();
        MsgMgr.getInstance().attach(this);
    }

    private synchronized void initObj() {
        userDetail = ModuleMgr.getCenterMgr().getMyInfo();
        videoVerifyBean = ModuleMgr.getCommonMgr().getVideoVerify();
        mIdCardVerifyStatusInfo = ModuleMgr.getCommonMgr().getIdCardVerifyStatusInfo();
    }

    private void initView() {
        findViewById(R.id.rl_auth_phone).setOnClickListener(this);
        findViewById(R.id.rl_auth_video).setOnClickListener(this);
        findViewById(R.id.rl_auth_id).setOnClickListener(this);
        tv_txt_auth_phone = (TextView) findViewById(R.id.txt_auth_phone);
        tv_txt_auth_video = (TextView) findViewById(R.id.txt_auth_video);
        tv_txt_auth_id = (TextView) findViewById(R.id.txt_auth_id);
    }

    private synchronized void initIdCardAuth() {
        tv_txt_auth_id.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_isnull));

        switch (mIdCardVerifyStatusInfo.getStatus()) {
            case 0:
                tv_txt_auth_id.setText(getResources().getString(R.string.auth_real_name));
                tv_txt_auth_id.setTextColor(ContextCompat.getColor(this, R.color.theme_color));
                break;
            case 1:
                tv_txt_auth_id.setText(getResources().getString(R.string.txt_authstatus_authing));
                tv_txt_auth_id.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_ing));
                break;
            case 2:
                tv_txt_auth_id.setText(getResources().getString(R.string.center_phone_has_verify));
                tv_txt_auth_id.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_complete));
                break;
            case 3:
            case 4:
                tv_txt_auth_id.setText(getResources().getString(R.string.txt_authstatus_autherror));
                tv_txt_auth_id.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_error));
                break;
            default:
                break;
        }
    }

    private synchronized void initVideoAuth() {
        tv_txt_auth_video.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_isnull));

        switch (videoVerifyBean.getStatus()) {
            case 0:
                tv_txt_auth_video.setText(getResources().getString(R.string.auth_real_video));
                tv_txt_auth_video.setTextColor(ContextCompat.getColor(this, R.color.theme_color));
                break;
            case 1:
                tv_txt_auth_video.setText(getResources().getString(R.string.txt_authstatus_authing));
                tv_txt_auth_video.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_ing));
                break;
            case 2:
                tv_txt_auth_video.setText(getResources().getString(R.string.txt_authstatus_autherror));
                tv_txt_auth_video.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_error));
                break;
            case 3:
                tv_txt_auth_video.setText(getResources().getString(R.string.center_phone_has_verify));
                tv_txt_auth_video.setTextColor(ContextCompat.getColor(this, R.color.txt_auth_status_complete));
                ModuleMgr.getCommonMgr().setVideochatConfig(true, true, this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_auth_phone://手机认证页
                if (!ModuleMgr.getCenterMgr().getMyInfo().isVerifyCellphone()) {
                    UIShow.showPhoneVerifyAct(MyAuthenticationAct.this, AUTHENTICSTION_REQUESTCODE);
                }
                break;
            case R.id.rl_auth_video://自拍认证
                UIShow.showMyAuthenticationVideoAct(this, authForVodeo);
                break;
            case R.id.rl_auth_id://身份认证
                UIShow.showIDCardAuthenticationAct(this, authIDCard);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initObj();
        if (requestCode == authForVodeo) {
            initVideoAuth();
        } else if (requestCode == authIDCard) {
            if (data == null) {
                return;
            }
            int back = data.getIntExtra(IDCardAuthenticationSucceedAct.IDCARDBACK, 0);
            if (back == 1) this.finish();
        }
    }

    private void initConfig() {
        //已完成认证，不再请求
        if (videoVerifyBean.getStatus() != 3) {// 3 已认证
            ModuleMgr.getCommonMgr().requestVideochatConfig(this);
        }
        if (mIdCardVerifyStatusInfo.getStatus() != 2) {// 2 已认证
            ModuleMgr.getCommonMgr().getVerifyStatus(this);
        }
    }

    /**
     * 手机已认证显示
     */
    private void phoneShow() {
        try {
            if (userDetail != null && userDetail.isVerifyCellphone()) {
                String mobile = userDetail.getCell_phone();
                if (!TextUtils.isEmpty(mobile)) {
                    tv_txt_auth_phone.setText(mobile.substring(0, 3) + "****" + mobile.substring(7, mobile.length()));
                    tv_txt_auth_phone.setTextColor(ContextCompat.getColor(this, R.color.gray_text));
                }
            } else {
                tv_txt_auth_phone.setText(getResources().getString(R.string.auth_bind_phone));
                tv_txt_auth_phone.setTextColor(ContextCompat.getColor(this, R.color.theme_color));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        initObj();
        initVideoAuth();
        initIdCardAuth();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_MyInfo_Change:
                phoneShow();
                break;
        }
    }
}
